﻿using Microsoft.AspNetCore.Mvc;
using SMes.DbMethod.IServices;
using SMes.DbMethod.Services;
using SMes.DbMethod.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VolPro.Core.Middleware;
using VolPro.Core.Utilities;

namespace VolPro.WebApi.Controllers.IotAPI
{
    /// <summary>
    /// API接受iot的数据，将数据进行处理。。。。
    /// </summary>
    //[AllowAnonymous]
    [ApiController]
    [Route("APi/Iot/SendEquipmentDataToMes")]
    public class IotAPIController(equipmentoperationlogsService equipmentoperationlogsService,
                                  equipmentfailurerecordsService equipmentfailurerecordsService,
                                  equipmentstatuslogsService equipmentstatuslogsService) : Controller
    {
        private readonly equipmentoperationlogsService _equipmentoperationlogsService = equipmentoperationlogsService;
        private readonly equipmentfailurerecordsService _equipmentfailurerecordsService = equipmentfailurerecordsService;
        private readonly equipmentstatuslogsService _equipmentstatuslogsService = equipmentstatuslogsService;
        /// <summary>
        /// 故障数据-故障处理
        /// </summary>
        /// <param name="dataApiViewModel">api数据</param>
        /// <returns>响应的结果</returns>
        [ActionLog("IOT故障处理")]
        [HttpPost("EquipmentFault")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> EquipmentFault([FromBody] IotDataApiViewModel dataApiViewModel)
        {
            ///业务处理---数据库增删改  tag =20 故障
            //WebResponseContent webResponseContent = new WebResponseContent();
            //var datas = dataApiViewModel.datas.Where(x => x.Tag == 20).ToList();
            //var errorDatas = dataApiViewModel.datas.Where(x => x.Tag != 20).Select(x => x.EquipmentCode).ToList();
            //if (errorDatas.Count > 0)
            //{
            //    return Json(webResponseContent.Error($"设备编码{string.Join(";", errorDatas)}的Tag不是故障"));
            //}
            var response = await _equipmentfailurerecordsService.AddEquipmentFault(dataApiViewModel.datas);
            // webResponseContent.OK("success");
            // return Json(webResponseContent);
            return Json(response);
        }

        [ActionLog("IOT设备状态")]
        [HttpPost("EquipmentStatus")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<IActionResult> EquipmentStatus([FromBody] IotDataApiViewModel dataApiViewModel)
        {
            ///业务处理---数据库增删改 tag=30 设备状态变化
            //WebResponseContent webResponseContent = new WebResponseContent();
            //var datas = dataApiViewModel.datas.Where(x => x.Tag == 30).ToList();
            //var errorDatas = dataApiViewModel.datas.Where(x => x.Tag != 30).Select(x => x.EquipmentCode).ToList();
            //if (errorDatas.Count > 0)
            //{
            //    return Json(webResponseContent.Error($"设备编码{string.Join(";", errorDatas)}的Tag不是设备状态变化"));
            //}
            var response = await _equipmentstatuslogsService.AddEqStatuslogs(dataApiViewModel.datas);
            //webResponseContent.OK("success");
            return Json(response);
        }

        [ActionLog("IOT设备相关数据")]
        [HttpPost("EquipmentData")]
        [ApiExplorerSettings(GroupName = "v2")]
        public IActionResult EquipmentData([FromBody] IotDataApiViewModel dataApiViewModel)
        {
            ///业务处理---数据库增删改
            WebResponseContent webResponseContent = new WebResponseContent();
            webResponseContent.OK("success");
            return Json(webResponseContent);
        }
        /// <summary>
        /// 暂时不处理
        /// </summary>
        [ActionLog("IOT能耗数据")]
        [HttpPost("EnergyConsumptionData")]
        [ApiExplorerSettings(GroupName = "v2")]
        public IActionResult EnergyConsumptionData([FromBody] IotDataApiViewModel dataApiViewModel)
        {
            ///业务处理---数据库增删改
            WebResponseContent webResponseContent = new WebResponseContent();
            webResponseContent.OK("success");
            return Json(webResponseContent);
        }
        [ActionLog("IOT工艺数据")]
        [HttpPost("ProcessData")]
        [ApiExplorerSettings(GroupName = "v2")]
        public IActionResult ProcessData([FromBody] IotDataApiViewModel dataApiViewModel)
        {
            ///业务处理---数据库增删改
            WebResponseContent webResponseContent = new WebResponseContent();
            webResponseContent.OK("success");
            return Json(webResponseContent);
        }
        [ActionLog("IOT质量数据")]
        [HttpPost("QualityData")]
        [ApiExplorerSettings(GroupName = "v2")]
        public IActionResult QualityData([FromBody] IotDataApiViewModel dataApiViewModel)
        {
            ///业务处理---数据库增删改
            WebResponseContent webResponseContent = new();
            webResponseContent.OK("success");
            return Json(webResponseContent);
        }
        [ActionLog("IOT报工数据")]
        [HttpPost("CirculationReportData")]
        [ApiExplorerSettings(GroupName = "v2")]
        public async Task<WebResponseContent> CirculationReportData([FromBody] IotDataApiViewModel dataApiViewModel)
        {
            ///业务处理---数据库增删改
            string eqcode = "system";
            string lotno = string.Empty;
            var status = -1;
            foreach (var item in dataApiViewModel.datas)
            {
                if (item.Tag == 10)
                {
                    eqcode = item.EquipmentCode;
                    lotno = item.Value.ToString() ?? string.Empty;
                }
                else if (item.Tag == 11)
                {
                    status = Convert.ToInt32(item.Value);
                }
            }
            if (status < 0)
            {
                return await _equipmentoperationlogsService.StartWorking(lotno);
            }
            return await _equipmentoperationlogsService.ReportWorking(lotno, eqcode, status);
            ///业务处理---数据库增删改            
        }
    }
}
